# 导包
import requests
import unittest
from utils import common_assert
import app
from api.login import LoginAPI
# 创建测试类
class TestLogin(unittest.TestCase):
    # 前置处理
    def setUp(self):
        self.api_login = LoginAPI()     # 实例化接口对象

    # 后置处理
    # def tearDown(self):
    #     pass
    # 创建测试方法
    # 登录成功
    def test001_login_success(self):
        # 调用登录接口获取信息，并进行断言
        response = self.api_login.login({"mobile":"13800000002","password":"888itcast.CN764%..."})
        print(response.json())
        # self.assertEqual(200,response.status_code)
        # # self.assertIn("application/json",response.headers.get("Content-Tyoe"))
        # self.assertEqual(True,response.json().get("success"))
        # self.assertEqual(10000,response.json().get("code"))
        # self.assertIn("操作成功！",response.json().get("message"))
        common_assert(self,response)
        # 提取token信息
        app.TOKEN = "Bearer " + response.json().get("data")
        print(app.TOKEN)
        app.headers_data["Authorization"] = app.TOKEN
        print(app.headers_data["Authorization"])
    # 登录失败，不输入手机号
    def test002_user_is_null(self):
        response = self.api_login.login({"mobile":"","password":"888itcast.CN764%..."})
        print(response.json())
        self.assertEqual(200, response.status_code)
        self.assertEqual(False, response.json().get("success"))
        self.assertEqual(20001, response.json().get("code"))
        self.assertIn("用户名或密码错误", response.json().get("message"))
    # 登录失败，不输入密码
    def test003_password_is_null(self):
        response = self.api_login.login({"mobile":"13800000002","password":""})
        print(response.json())
        self.assertEqual(200, response.status_code)
        self.assertEqual(False, response.json().get("success"))
        self.assertEqual(20001, response.json().get("code"))
        self.assertIn("用户名或密码错误", response.json().get("message"))
    # 登录失败，手机号长度小于11位
    def test004_number_less_11(self):
        response = self.api_login.login({"mobile":"1380000002","password":"888itcast.CN764%..."})
        print(response.json())
        self.assertEqual(200, response.status_code)
        self.assertEqual(False, response.json().get("success"))
        self.assertEqual(20001, response.json().get("code"))
        self.assertIn("用户名或密码错误", response.json().get("message"))
        # 登录失败，手机号长度大于11位
    def test005_number_greater_11(self):
        response = self.api_login.login({"mobile":"138000000002","password":"888itcast.CN764%..."})
        print(response.json())
        self.assertEqual(200, response.status_code)
        self.assertEqual(False, response.json().get("success"))
        self.assertEqual(20001, response.json().get("code"))
        self.assertIn("用户名或密码错误", response.json().get("message"))
    # 登录失败，手机号输入非数字
    def test006_user_not_number(self):
        response = self.api_login.login({"mobile":"138000000a2","password":"888itcast.CN764%..."})
        print(response.json())
        self.assertEqual(200, response.status_code)
        self.assertEqual(False, response.json().get("success"))
        self.assertEqual(20001, response.json().get("code"))
        self.assertIn("用户名或密码错误", response.json().get("message"))
    # 登录失败，手机号未注册
    def test007_user_not_register(self):
        response = self.api_login.login({"mobile":"13100000002","password":"888itcast.CN764%..."})
        print(response.json())
        self.assertEqual(200, response.status_code)
        self.assertEqual(False, response.json().get("success"))
        self.assertEqual(20001, response.json().get("code"))
        self.assertIn("用户名或密码错误", response.json().get("message"))
    # 多参
    def test008_login_duo_parameter(self):
        # 调用登录接口获取信息，并进行断言
        response = self.api_login.login({"mobile":"13800000002","password":"888itcast.CN764%...","params":"hahaha"})
        print(response.json())
        self.assertEqual(200,response.status_code)
        self.assertEqual(True,response.json().get("success"))
        self.assertEqual(10000,response.json().get("code"))
        self.assertIn("操作成功！",response.json().get("message"))
    # 少参-手机号
    def test009_login_shao_parameter(self):
        # 调用登录接口获取信息，并进行断言
        response = self.api_login.login({"password":"888itcast.CN764%..."})
        print(response.json())
        self.assertEqual(200, response.status_code)
        self.assertEqual(False, response.json().get("success"))
        self.assertEqual(20001, response.json().get("code"))
        self.assertIn("用户名或密码错误", response.json().get("message"))
    # 少参-密码
    def test010_login_shao_parameter(self):
        # 调用登录接口获取信息，并进行断言
        response = self.api_login.login({"mobile":"13800000002"})
        print(response.json())
        self.assertEqual(200, response.status_code)
        self.assertEqual(False, response.json().get("success"))
        self.assertEqual(20001, response.json().get("code"))
        self.assertIn("用户名或密码错误", response.json().get("message"))
    # 无参
    def test011_login_shao_parameter(self):
        # 调用登录接口获取信息，并进行断言
        response = self.api_login.login({})
        print(response.json())
        self.assertEqual(200, response.status_code)
        self.assertEqual(False, response.json().get("success"))
        self.assertEqual(20001, response.json().get("code"))
        self.assertIn("用户名或密码错误", response.json().get("message"))
    # 拼写错误-手机号参数拼写错误
    def test012_login_shao_parameter(self):
        # 调用登录接口获取信息，并进行断言
        response = self.api_login.login({"mobiel":"13800000002","password":"888itcast.CN764%..."})
        print(response.json())
        self.assertEqual(200, response.status_code)
        self.assertEqual(False, response.json().get("success"))
        self.assertEqual(20001, response.json().get("code"))
        self.assertIn("用户名或密码错误", response.json().get("message"))
    #拼写错误-密码拼写错误
    def test013_login_shao_parameter(self):
        # 调用登录接口获取信息，并进行断言
        response = self.api_login.login({"mobile":"13800000002","passwd":"888itcast.CN764%..."})
        print(response.json())
        self.assertEqual(200, response.status_code)
        self.assertEqual(False, response.json().get("success"))
        self.assertEqual(20001, response.json().get("code"))
        self.assertIn("用户名或密码错误", response.json().get("message"))
